


#include <stdio.h>
#include "sc16is7xx.h"
#include "cycbuf.h"
#include "SEGGER_RTT.h"

extern struct i2c_slave_data_t i2c_data;

CYC_BUF_T sc16is7xx_txbuf[4];
CYC_BUF_T sc16is7xx_rxbuf[4];

void sc16is7xx_init(void)
{
  CYC_BUF_RESET(sc16is7xx_txbuf[0]);
  CYC_BUF_RESET(sc16is7xx_rxbuf[0]);
}

void sc16is7xx_gpio_set(unsigned int offset,unsigned int val)
{
    
}


unsigned char sc16is7xx_gpio_get(unsigned int offset)
{
    return  0;
}

void sc16is7xx_gpio_direction_output(unsigned int offset,unsigned int val)
{
  
}

unsigned int sc16is7xx_gpio_direction_input(unsigned int offset)
{
    return 0;
}


unsigned int sc16is7xx_ops_write(unsigned char *regaddr,unsigned char *val)
{
  unsigned char tval;
  unsigned char reg = *regaddr;

  unsigned int i;
  unsigned char ch = (reg & 0x0F);
  switch((reg & 0xF0)/16)
  {

    case SC16IS7XX_THR_REG:/*0x00 W*/
      #if 0
      if(((i2c_data.i2cs_memory[SC16IS7XX_LCR_REG] & 0x80) == 0x80) && \
        (i2c_data.i2cs_memory[SC16IS7XX_LCR_REG] != 0xbf))
      {
        // 波特率寄存器
        SEGGER_RTT_printf(0,"set baud\n");
      }
      else if(i2c_data.i2cs_memory[SC16IS7XX_LCR_REG] == 0xbf)
      {
      }
      else
      #endif
      {  
        SEGGER_RTT_printf(0,"tx %02x\n",*val);
        if(!CYC_BUF_FULL(sc16is7xx_txbuf[ch]))    
          CYC_BUF_WR(sc16is7xx_txbuf[ch],*val);   // 写入数据到发送缓冲区
        else
          ;   // 缓冲区溢出，如何处理
      }
      break;
   
    case SC16IS7XX_IER_REG:     // 中断使能寄存器
      if(((i2c_data.i2cs_memory[SC16IS7XX_LCR_REG] & 0x80) == 0x80) && \
        (i2c_data.i2cs_memory[SC16IS7XX_LCR_REG] != 0xbf))
      {
        // 波特率寄存器
        SEGGER_RTT_printf(0,"set baud\n");
      }
      else if(i2c_data.i2cs_memory[SC16IS7XX_LCR_REG] == 0xbf)
      {
      }  
      break;
    case SC16IS7XX_FCR_REG:/*0x02 W*/  //SC16IS7XX_IIR_REG                  // 写FIFO控制寄存器
      tval = *val;
      if(tval & 0x02)         // RX FIFO复位
      {
      }
      if(tval & 0x04)        // TX FIFO复位
      {
      }
      if(tval & 0x30)        // TX触发点
      {
      }
      if(tval & 0xC0)         // RX触发点
      {
      }
      break;
    case SC16IS7XX_LCR_REG:/*0x03*/         //线控制寄存器
      
      break;
    case SC16IS7XX_MCR_REG:/*0x04*/
      break;
    case SC16IS7XX_LSR_REG:/*0x05*/
      tval = (*val)^i2c_data.i2cs_memory[reg];   // 线状态寄存器
      break;
    case SC16IS7XX_MSR_REG:/*0x06*/       // READ
      break;
    case SC16IS7XX_IOSTATE_REG: /*0x0b*/
    {
      SEGGER_RTT_printf(0,"Send %02x to Reg[%02x]\n",*val,reg);
      tval = (*val)^i2c_data.i2cs_memory[reg];
      //printf("%02x\n",tval);    
      if(tval != 0) // set val ^ memory 
      {
        //printf("%02x %02x to Reg[%02x]\n",*val,i2c_data.i2c_memory[reg],reg);
        for(i=0;i<8;i++)  {
           if(tval & (1<<i) )  {
             if(*val & (1<<i)){
               SEGGER_RTT_printf(0,"set bit %d\n",i);
               
             }else{
               SEGGER_RTT_printf(0,"clr bit %d\n",i);
               
             }
           }
        }
      }
    }
    default:
      SEGGER_RTT_printf(0,"wr %02x,%02x\n",reg,val[0]);
      break;        
    //sc16is7xx_gpio_set(1,0);
  }
  
  
  i2c_data.i2cs_memory[reg] = *val;  // 处理完数据
  return 0;
}

unsigned int sc16is7xx_ops_read(unsigned char *regaddr,unsigned char *val)
{
  unsigned char tval = 0;
  unsigned char reg = *regaddr;
  unsigned char ch = reg & 0x0F;

  *val = tval;

  switch((reg & 0xf0) /16)
  { 
    case SC16IS7XX_RHR_REG:
      if(CYC_BUF_COUNT(sc16is7xx_rxbuf[ch]))      // 某个通道接收到数据
        tval = CYC_BUF_RD(sc16is7xx_rxbuf[ch]);     // 读取一个数据
      else
        tval = 0x00;          // 数据缓冲区没有数据，则返回0
      break;
    case SC16IS7XX_IER_REG:
      tval = i2c_data.i2cs_memory[0x01];
      break;
    case SC16IS7XX_MCR_REG:
      
      break;
    case SC16IS7XX_LSR_REG:
      break;
    case SC16IS7XX_IIR_REG:    //SC16IS7XX_IIR_REG:
      tval = i2c_data.i2cs_memory[0x02];   // 中断标志寄存器
      break;
    case SC16IS7XX_IOSTATE_REG:
      //printf("Read %02x to Reg[02x%]\n",*val,reg);
      //tval = i2c_data.i2cs_memory[reg];
      break;
    case SC16IS7XX_TXLVL_REG:
      //printf("Read Txlvl %02x\n", CYC_BUF_SIZE - CYC_BUF_COUNT(sc16is7xx_txbuf[0]));
      tval = CYC_BUF_SIZE - CYC_BUF_COUNT(sc16is7xx_txbuf[ch]);
      break;
     case SC16IS7XX_MSR_REG:/*0x06 R*/
    
    case SC16IS7XX_RXLVL_REG:
      tval = CYC_BUF_COUNT(sc16is7xx_txbuf[ch]);     // 某个通道接收到的数据
      break;

    default:
      printf("break\n");
      break;
  }

  *val = tval;
  return 0;
}

